package com.example.hewei.smarttransport.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hewei.smarttransport.Adapter.CarsStateGridAdapter;
import com.example.hewei.smarttransport.Beans.CarState;
import com.example.hewei.smarttransport.R;

import java.util.ArrayList;

/**
 * Created by 何伟 on 2017/3/8.
 */

public class AdminCars extends BaseFragment {
    GridView mCarsStateGrid;
    ArrayList<CarState> mCarsState = new ArrayList<CarState>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_admin_car, container,false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        getCarsState();
        setAdapter();
        setListener();
    }

    private void setListener() {
        mCarsStateGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView mCarIDTV= (TextView) view.findViewById(R.id.carID);
                Toast.makeText(getContext(),"setOnItemClickListener():CarID="+mCarIDTV.getText(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getCarsState() {
        mCarsState.clear();
        for(int i=1;i<=4; i++){
            CarState carState=new CarState();
            carState.setCarID(i+"");
            carState.setCarState(true);
            carState.setSpeed("100");
            carState.setBalance((100+i*10)+"");
            carState.setLocation("道路"+i);
            mCarsState.add(carState);
        }
    }

    private void setAdapter() {
        CarsStateGridAdapter adapter=new CarsStateGridAdapter(getContext(),0,mCarsState);
        mCarsStateGrid.setAdapter(adapter);
    }

    private void initView() {
        mCarsStateGrid=(GridView)getView().findViewById(R.id.carsState);
    }
}
